package sbu.cs;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class Settings {

	private static Settings settings;
	private Context context;
	private SharedPreferences sharedPrefs;
	private SharedPreferences.Editor editor;
	
	private static final String KEY_USERID = "USERID";
	private static final String DEF_USERID = "INVALID";
	
	public static final String KEY_LATITUDE = "LATITUDE";
	public static final String DEF_LATITUDE = "0.0";
	
	public static final String KEY_LONGITUDE = "LONGITUDE";
	public static final String DEF_LONGITUDE = "0.0";
	
	public static final int NOTIFICATION_ID  = 1;
	public static final int DB_UPDATE_TIMEOUT = 30 * 1000;
	
	public static final int DISTANCE_DELTA = 10; //10 meters
	
	public static final String KEY_LAST_MESSAGE = "LAST_MESSAGE";
	public static final String DEF_LAST_MESSAGE ="INV";
	
	public static final String PRIORITY_CRITICAL = "Critical";
	public static final String PRIORITY_HIGH = "High";
	public static final String PRIORITY_MEDIUM = "Medium";
	public static final String PRIORITY_LOW = "Low";
	
	public static final String CATEGORY_ALERTS = "Alerts";
	public static final String CATEGORY_ACADEMIC = "Academic";
	public static final String CATEGORY_SHOPPING = "Shopping";
	public static final String CATEGORY_SPORTS = "Sports";
	public static final String CATEGORY_FOOD = "Food";
	public static final String CATEGORY_FUN = "Fun";
	
	private Settings(Context context){
		doSet(context);
	}
	
	private void doSet(Context context){
		this.context = context;
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
		editor = sharedPrefs.edit();
	}
	
	public static Settings getPreferenceManager(Context context){
		if(settings == null){
			settings = new Settings(context);
		}
		return settings;
	}
	
	public String getUserId(){
		return sharedPrefs.getString(KEY_USERID, DEF_USERID);
	}
	
	public void setUserId(String id){
		editor.putString(KEY_USERID, id);
        editor.commit(); // Very important
	}
	public boolean isUserIdSet(){
		String regId = sharedPrefs.getString(KEY_USERID, DEF_USERID);
		if(regId == DEF_USERID){
			return false;
		}
		return true;
	}
	public void resetUserId(){
		editor.putString(KEY_USERID, DEF_USERID);
		editor.commit(); // Very important
	}
	public void resetRegInfo(){
		editor.clear();
		editor.commit();
	}

	public void startService(){
		context.startService(new Intent(context, VircastDaemon.class));
	}
	
	public Double getLatitude(){
		String latStr = sharedPrefs.getString(KEY_LATITUDE, DEF_LATITUDE);
		return new Double(Double.parseDouble(latStr));
	}
	
	public Double getLongitude(){
		String longitudeStr = sharedPrefs.getString(KEY_LONGITUDE, DEF_LONGITUDE);
		return new Double(Double.parseDouble(longitudeStr));
	}
	
	public void setLastMessage(String message){
		editor.putString(KEY_LAST_MESSAGE, message);
		editor.commit(); // Very important
	}
	
	public String getLastMessage(){
		String message = sharedPrefs.getString(KEY_LAST_MESSAGE, DEF_LAST_MESSAGE);
		return message;
	}

}
